package csc411;

import java.net.*;
import java.util.Calendar;

public class Observer {

	private InetAddress address;
	private int port;
	private int interval;
	private Calendar expiry;

	public Observer(InetAddress address, int port, int interval) {
		this.address = address;
		this.port = port;
		this.interval = interval;

		this.expiry = Calendar.getInstance();
		this.expiry.add(Calendar.SECOND, interval);

	}

	public InetAddress getAddress() {
		return this.address;
	}

	public int getPort() {
		return this.port;
	}

	public boolean hasExpired() {
		boolean expired = false;

		Calendar now = Calendar.getInstance();

		int diff = expiry.compareTo(now);

		if (diff <= 0) {
			expired = true;
		}

		return expired;
	}

	public void updateInterval(int interval) {
		this.interval = interval;

		this.expiry = Calendar.getInstance();
		this.expiry.add(Calendar.SECOND, interval);
	}

	public boolean equals(InetAddress address, int port) {

		if (this.address.equals(address) && this.port == port) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public String toString() {
		return this.address.getHostAddress() + ":" + this.port;
	}
}
